/*格式化小于10的数字*/
let formatTen = function (no) {
  let num = Number(no); //转换数字
  if (num < 10) {
      return '0' + num;
  }
  return num;
};
//格式化日期
let formatDate = function (date) {
  return date.getFullYear() + "-" + formatTen(date.getMonth() + 1) + "-" + formatTen(date.getDate()) + " " + formatTen(date.getHours()) + ":" + formatTen(date.getMinutes()) + ":" + formatTen(date.getSeconds());
};

//计算Object或者string的长度
let count = function(o) {
  let t = typeof o;
  if ('string' == t) {
      return o.length;
  } else if ('object' == t) {
      let n = 0;
      for (let i in o) {
          n++;
      }
      return n;
  }
  return false;
}

//深拷贝对象
let deepCopyObj = function(p, c) {
  var c = c || {};
　for (var i in p) {
  　if (typeof p[i] === 'object' && p[i] != null) {
      c[i] = (p[i].constructor === Array) ? [] : {};
      deepCopyObj(p[i], c[i]);
  　} else {
  　　c[i] = p[i];
    }
  }
　return c;
}

// 深拷贝对象数组
let deepCopyObjArray = function (arr) {
  arr = arr && arr.length > 0 ? arr : [];
  arr = arr.constructor == Array ? arr : [];
  return [].concat(JSON.parse(JSON.stringify(arr)));
}

//把JSON对象转成GET方法的字符串参数
let jsonToUrl = function (obj) {
  let result = '';
  for(let key in obj) {
    result += '&' + key + '=' + obj[key];
  }
  result = '?' + result.substring(1, result.length);
  return result;
}

let imgError = function(e, flag) {
  let src = '';
  switch(flag) {
    case 'car' : src = '@/assets/img/car.png';break;
    case 'license' : src = '@/assets/img/license.png';break;
    default: src = '@/assets/img/car.png';break;
  }
  e.src = src;
}

let getByteLength = function (str) {
  let realLength = 0;
  let len = str.length;
  let charCode = -1;
  for (var i = 0; i < len; i++) {
    charCode = str.charCodeAt(i);
    if (charCode >= 0 && charCode <= 128) realLength += 1;
    else realLength += 2;
  }
  return realLength;
}

let mileSecondToDate = function(value) {
  let result = '';
  if(value) {
    let date = new Date(value);
    result =  date.getFullYear() + "-" + formatTen(date.getMonth() + 1) + "-" + formatTen(date.getDate()) + " " + formatTen(date.getHours()) + ":" + formatTen(date.getMinutes()) + ":" + formatTen(date.getSeconds());
  }
  return result;
}

let langObj = JSON.parse(localStorage.getItem('langObj'));
let _day =  '天';
let _hour = '时';
let _minute = '分';
if(langObj) {
  _day =  langObj.normal_label_time_day || '天';
  _hour = langObj.normal_label_time_hour || '时';
  _minute = langObj.normal_label_time_minute || '分';
}

let minutesToHowLong = function(Val) {
  let mits = Val < 0 ? 0 : Val;
  //计算天数
  let days = Math.floor(mits / (60 * 24));

  //计算小时数
  let leave_d = mits % (60 * 24); //计算除去整天数后还剩下的分钟数
  let hours = Math.floor(leave_d / 60);

  //计算分钟数
  let minutes = leave_d % 60;
  
  if(days !== 0) {
    return '' + days + _day + hours + _hour + minutes + _minute;
  } else if (hours !== 0) {
    return '' + hours + _hour + minutes + _minute;
  } else {
    return '' + minutes + _minute;
  }
}

export {
  formatTen,
  formatDate,
  count,
  deepCopyObj,
  deepCopyObjArray,
  imgError,
  jsonToUrl,
  getByteLength,
  mileSecondToDate,
  minutesToHowLong
}
